<?php
// 优惠模型
class CouponModel extends CommonModel {
	protected $tableName = 'coupon';
	protected $_validate = array(

	);
	
    protected $_auto =	array(
//            	array('XS_XH','getStuXH',1,'callback'),
//     		array('XS_PY','getStuPY',3,'callback'),
    		array('status','1',self::MODEL_INSERT),  // 新增的时候把status字段设置为1 活跃
        //array('create_time','time',self::MODEL_INSERT,'function'),
    );
	
	public function getModeDisplayList($xqid,$LX,$CJR,$YHKMC,$JE_GREATER,$JE_LESS,$KSSJ_START,$JSSJ_END,$offset,$rows,$sort,$order){
	
		$result = array();
		$where ="1";
		if(!empty($xqid)){
			$where .= " and YHK_XQ in(". $xqid .")";
		}

		if(!empty($LX)){
			$where .= " and  YBK_LX='".$LX."'";
		}
// 		if(!empty($CJR)){
			$CJR = mysql_real_escape_string($CJR);
			$where .= " AND YHK_CJRMC like '%".$CJR."%'";
// 		}
// 		if(!empty($YHKMC)){
			$YHKMC = mysql_real_escape_string($YHKMC);
			$where .= " AND YHK_MC like '%".$YHKMC."%'";
// 		}
		if(!empty($JE_GREATER)){
			$where .= " and  YHK_JE >=".$JE_GREATER;
		}
		if(!empty($JE_LESS)){
			$where .= " and  YHK_JE <=".$JE_LESS;
		}
		if(!empty($KSSJ_START)){
			$where .= " and  YHK_KSSJ >=".$KSSJ_START;
		}
		if(!empty($JSSJ_END)){
			$where .= " and  YHK_JSSJ <=".$JSSJ_END;
		}
	
		$sql = "select t.*,t1.ITEM_VALUE as XQ,t2.ITEM_VALUE  AS LX,t4.nickname AS CJR
					from  (select * from ".$this->tablePrefix."coupon  where " . $where . " and status=1 ) as t
					join ".$this->tablePrefix."dictionary_list t1 on t.yhk_xq = t1.ITEM_KEY
					join ".$this->tablePrefix."dictionary_list t2 on t.yhk_lx = t2.ITEM_KEY
					join ".$this->tablePrefix."user t4 on t.yhk_cjrid=t4.id order by t." . $sort . " " . $order;
	
		$arrNum = $this->db->query("select count(*) as num from ".$this->tablePrefix."coupon where " . $where);
		$row = $arrNum[0]['num'];
	
		$result["total"] = $row;
		$rs = $this->db->query($sql . " limit $offset,$rows");
// 		$items = array();
// 		while($row = mysql_fetch_object($rs)){
// 			array_push($items, $row);
// 		}
		$result["rows"] = $rs;
	
		return $result;
	}
	
	//自动生成账目流水编号（报名单号）
	public function getCouponLSBH($xq_id,$YHK_CLSJ){
		$YHK_BH = $xq_id . $YHK_CLSJ  ;
		$map['YHK_BH'] = array('like',"%".$YHK_BH."%");
		$Order = M ( "Order" );
		$Orders = array();
		$Orders = $Order->where($map)->limit (0,1)->order ( 'YHK_BH desc' )->select();
		if(count($Orders)!=0){
			$ord = $Orders[0];
			$maxBH = $ord['YHK_BH'];
			
			$lastNum = intval(substr($maxBH,-5));//后5位流水号
			$lastNum += 1;
			if($lastNum<10){
				$YHK_BH = $YHK_BH.'0000'.$lastNum;
			}else if($lastNum<100){
				$YHK_BH = $YHK_BH.'000'.$lastNum;
			}else if($lastNum<1000){
				$YHK_BH = $YHK_BH.'00'.$lastNum;
			}else{
				$YHK_BH = $YHK_BH.$lastNum;
			}		
		}else{
			$YHK_BH = $YHK_BH . '00001' ;
		}
		return  $YHK_BH;
	}
	
}